<?php
if(!defined("__ZBXE__")) exit();

/**
 * @lazyload.addon.php
 * @author 카르마(http://backupxe.com)
 *
 **/


if($called_position == 'before_display_content' && Context::getResponseMethod() == 'HTML')
{
	$pattern = '@<img\s*src\s*=\s*(["\'])([^\s>]+?)\1@i';
	$pattern_documents_area = '/<!--Before(Document|Comment)\([0-9]+,[0-9]+\)-->(.*?)<!--After(Document|Comment)\([0-9]+,[0-9]+\)-->/is';
	$_imgcount =0;
	
	
	preg_match_all($pattern_documents_area,$output,$matches);
	if(!count($matches[0])) return;
	
	foreach($matches[0] as $val) 
	{
		preg_match_all($pattern,strip_tags($val,'<img>'),$match);
		if(!count($match)) return;
		for($i=0;$i<count($match[0]);$i++) 
		{
			$_imgcount++;
			$repl = sprintf('<img class="lazy" src="%s" data-original="%s"','./addons/lazyload/img/grey.gif',$match[2][$i]);
			
			$output = str_replace($match[0][$i],$repl,$output);
		}
	}
	if($_imgcount) {
		Context::addCSSFile("./addons/lazyload/css/lazyload.css", false);
		Context::addCSSFile('./addons/lazyload/css/lightbox.css',false);
		Context::addJsFile('./addons/lazyload/js/lazyload.js',false,'',null,'body');
		Context::addJsFile('./addons/lazyload/js/lightbox_plus.js',false,'',null,'body');
		Context::addJsFile('./addons/lazyload/js/normal.js', false,'',null,'body');
	}
}